package com.jzqf.harmonydemo.library.media;

import com.jzqf.harmonydemo.ResourceTable;
import com.jzqf.harmonydemo.base.BaseAbilitySlice;
import com.jzqf.harmonydemo.library.media.api.ImplPlayer;
import com.jzqf.harmonydemo.library.media.enums.Constants;
import com.jzqf.harmonydemo.library.media.view.PlayerLoading;
import com.jzqf.harmonydemo.library.media.view.PlayerView;
import com.jzqf.harmonydemo.library.media.view.SimplePlayerController;
import com.jzqf.harmonydemo.utils.ScreenUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DependentLayout;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.bundle.AbilityInfo;

/**
 * 视频播放器
 *
 * @author LiuWeiHao
 * 2021/5/26 9:09
 */
public class MediaPlayerAbilitySlice extends AbilitySlice {
    private String url = "entry/resources/base/media/gubeishuizhen.mp4";
    private ImplPlayer player;
    private PlayerView playerView;
    private PlayerLoading loadingView;
    private DependentLayout rootLayout;
    private SimplePlayerController simplePlayerController;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_media_player);
        initView();
        initPlayer();
    }

    private void initPlayer() {
        player = new HmPlayer.Builder(this).setFilePath(url).create();
        player.getLifecycle().onStart();
        playerView.bind(player);
        loadingView.bind(player);
        simplePlayerController.bind(player);
    }

    private void initView() {
        rootLayout = (DependentLayout) findComponentById(ResourceTable.Id_root_layout);
        playerView = (PlayerView) findComponentById(ResourceTable.Id_player_view);
        loadingView = (PlayerLoading) findComponentById(ResourceTable.Id_loading_view);
        simplePlayerController = (SimplePlayerController) findComponentById(ResourceTable.Id_controller_view);
    }


    @Override
    public void onActive() {
        super.onActive();
        getGlobalTaskDispatcher(TaskPriority.DEFAULT).delayDispatch(() -> player.play(), Constants.NUMBER_1000);
    }

    @Override
    public void onForeground(Intent intent) {
        player.getLifecycle().onForeground();
        super.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        player.getLifecycle().onBackground();
        super.onBackground();
    }

    @Override
    protected void onStop() {
        loadingView.unbind();
        simplePlayerController.unbind();
        playerView.unbind();
        player.getLifecycle().onStop();
        super.onStop();
    }

    @Override
    protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
        super.onOrientationChanged(displayOrientation);
        int screenWidth = ScreenUtil.getScreenWidth(this);
        rootLayout.setWidth(screenWidth);
        player.openGesture(displayOrientation == AbilityInfo.DisplayOrientation.LANDSCAPE);
    }
}
